package com.thinkgem.dingdan.modules.bu.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.dingdan.common.config.Global;
import com.thinkgem.dingdan.common.persistence.Page;
import com.thinkgem.dingdan.common.utils.StringUtils;
import com.thinkgem.dingdan.common.web.BaseController;
import com.thinkgem.dingdan.modules.bu.entity.BuSupplier;
import com.thinkgem.dingdan.modules.bu.service.BuSupplierService;
import com.thinkgem.dingdan.modules.sys.entity.User;
import com.thinkgem.dingdan.modules.sys.service.IUserService;

/**
 * Controller
 * @author
 * @version 2018-08-26
 */
@Controller
@RequestMapping(value = "${adminPath}/bu/buSupplier")
public class BuSupplierController extends BaseController {

	@Autowired
	private BuSupplierService buSupplierService;

	@Resource
	private IUserService userService;
	
	@ModelAttribute
	public BuSupplier get(@RequestParam(required=false) String id) {
		BuSupplier entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = buSupplierService.get(id);
		}
		if (entity == null){
			entity = new BuSupplier();
		}
		return entity;
	}
	
	@RequiresPermissions("bu:buSupplier:view")
	@RequestMapping(value = {"list", ""})
	public String list(BuSupplier buSupplier, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BuSupplier> page = buSupplierService.findPage(new Page<BuSupplier>(request, response), buSupplier); 
		model.addAttribute("page", page);
		return "modules/bu/buSupplierList";
	}

	@RequiresPermissions("bu:buSupplier:view")
	@RequestMapping(value = "form")
	public String form(BuSupplier buSupplier, Model model) {
		User user = new User();
		List<User> list = (List<User>)userService.findList(user);
		model.addAttribute("salesManList", list);
		model.addAttribute("buSupplier", buSupplier);
		return "modules/bu/buSupplierForm";
	}

	@RequiresPermissions("bu:buSupplier:edit")
	@RequestMapping(value = "save")
	public String save(BuSupplier buSupplier, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, buSupplier)){
			return form(buSupplier, model);
		}
		buSupplierService.save(buSupplier);
		addMessage(redirectAttributes, "保存业务员成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buSupplier/?repage";
	}
	
	@RequiresPermissions("bu:buSupplier:edit")
	@RequestMapping(value = "delete")
	public String delete(BuSupplier buSupplier, RedirectAttributes redirectAttributes) {
		buSupplierService.delete(buSupplier);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buSupplier/?repage";
	}

}